package com.hy.memoryassistant;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditNoteDetailFragment extends Fragment {

	public static String ARG_ITEM_ID = "ARG_ITEM_ID";
	public static String TAG = "EditNoteDetailFragment";

	private ReminderSQLCollection mCollection;
	private Reminder mReminder;

	public EditNoteDetailFragment() {

	}

	/**
	 * override onCreate call to get fragment arguments etc
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mCollection = ReminderSQLCollection.getInstance(getActivity());

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			long itemId = getArguments().getLong(ARG_ITEM_ID);
			Log.d(TAG, String.format("ARG_ITEM_ID: %s", itemId));
			if (itemId != 0)
				mReminder = mCollection.getById(itemId);
			else
				mReminder = new Reminder();
		} else {
			mReminder = new Reminder();
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_edit_note_detail,
				container, false);

		Log.d(TAG, "Creating view");

		// find edit views on layout
		final EditText titleView = ((EditText) rootView
				.findViewById(R.id.edit_reminder_detail_title_value));
		final EditText promptView = ((EditText) rootView
				.findViewById(R.id.edit_reminder_detail_prompt_value));
		final EditText summaryView = ((EditText) rootView
				.findViewById(R.id.edit_reminder_detail_summary_value));
		final EditText tagView = ((EditText) rootView
				.findViewById(R.id.edit_reminder_detail_tags_value));
		final EditText extLinkView = ((EditText) rootView
				.findViewById(R.id.edit_reminder_detail_ext_link_value));
		final EditText mediaView = ((EditText) rootView
				.findViewById(R.id.edit_reminder_detail_media_type_value));
		
		// populate layout with existing reminder
		titleView.setText(mReminder.title);
		promptView.setText(mReminder.prompt);
		summaryView.setText(mReminder.summary);
		tagView.setText(mReminder.tag);
		extLinkView.setText(mReminder.ext_link);
		mediaView.setText(String.format("%d", mReminder.media_type));

		((Button) rootView.findViewById(R.id.edit_reminder_detail_save))
				.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Log.d(TAG, "Save Clicked.");
                        mReminder.title = titleView.getText().toString();
                        mReminder.prompt = promptView.getText().toString();
                        mReminder.summary = summaryView.getText().toString();
                        mReminder.tag = tagView.getText().toString();
                        mReminder.ext_link = extLinkView.getText().toString();
                        // TODO: media type not working, not sure why
                        // mReminder.media_type =
                        // mediaView.getText().toString();
                        mReminder.date_added = System.currentTimeMillis();
                        mReminder.last_modified = System.currentTimeMillis();
                        mReminder.next_notification = System.currentTimeMillis();


                        mCollection.add(mReminder);
                        Toast.makeText(getActivity(), "Saved",
                                Toast.LENGTH_SHORT).show();


                        getActivity().finish();
                        // getActivity().navigateUpTo(
                        // new Intent(getActivity(),
                        // NoteListActivity.class));

                    }
                });


		return rootView;
	}

}
